package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gallant
 * @create 2020-06-29 19:02
 */
@Service(interfaceName = "com.itheima.service.CheckItemService")
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> checkItems = checkItemDao.queryByCondition(queryString);
        return new PageResult(checkItems.getTotal(),checkItems.getResult());
    }

    // 添加
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    // 编辑前查询
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    // 编辑
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    // 删除
    public void delete(Integer id) {
        // 在service层进行逻辑处理
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0){
            throw new RuntimeException("此检查项被引用,不能删除!");
        }else {
            checkItemDao.delete(id);
        }
    }

    @Override
    public List findAll() {
        return checkItemDao.findAll();
    }

}
